package com.sls.web.manager.action.recharge;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.recharge.pojo.SupplierDenomination;
import com.sls.web.manager.recharge.pojo.SupplierRechargeMode;
import com.sls.web.manager.recharge.querybean.SupplierDenominationQuery;
import com.sls.web.manager.recharge.service.SupplierDenominationService;
import com.sls.web.manager.recharge.service.SupplierRechargeModeService;

public class SupplierDenominationAction extends SuperAction {
	private static final long serialVersionUID = 1594763427912983049L;
	@Resource(name = "supplierDenominationService")
	private SupplierDenominationService supplierDenominationService;
	@Resource(name = "supplierRechargeModeService")
	private SupplierRechargeModeService supplierRechargeModeService;
	/**
	 * 返回分页对象
	 */
	public Pagination<?> page;
	private SupplierDenominationQuery supplierDenominationQuery;
	private SupplierDenomination supplierDenomination;
	private String id;
	private String productId;
	private String modeId;
	private String faceValue;
	private String costPrice;
	private String maxFaceValue;

	public Pagination<?> getPage() {
		return page;
	}

	public SupplierDenominationQuery getSupplierDenominationQuery() {
		return supplierDenominationQuery;
	}

	public void setSupplierDenominationQuery(
			SupplierDenominationQuery supplierDenominationQuery) {
		this.supplierDenominationQuery = supplierDenominationQuery;
	}

	public SupplierDenomination getSupplierDenomination() {
		return supplierDenomination;
	}

	public void setSupplierDenomination(
			SupplierDenomination supplierDenomination) {
		this.supplierDenomination = supplierDenomination;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getModeId() {
		return modeId;
	}

	public void setModeId(String modeId) {
		this.modeId = modeId;
	}

	public String getFaceValue() {
		return faceValue;
	}

	public void setFaceValue(String faceValue) {
		this.faceValue = faceValue;
	}

	public String getCostPrice() {
		return costPrice;
	}

	public void setCostPrice(String costPrice) {
		this.costPrice = costPrice;
	}

	public String getMaxFaceValue() {
		return maxFaceValue;
	}

	public void setMaxFaceValue(String maxFaceValue) {
		this.maxFaceValue = maxFaceValue;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	// 添加
	public String add() {
		return "edit";
	}

	// 添加供货商提供的充值面额
	public String addDenomination() throws Exception {
		try {
			supplierDenomination.setFaceValue((int) (Double
					.parseDouble(faceValue) * 1000));
			supplierDenomination.setCostPrice((int) (Double
					.parseDouble(costPrice) * 1000));
			if (StringUtils.isNotEmpty(maxFaceValue))
				supplierDenomination.setMaxFaceValue((int) (Double
						.parseDouble(maxFaceValue) * 1000));
			supplierDenominationService.add(supplierDenomination,
					this.getCurrentOperator());
			redirectionUrl = "supplier_denomination!list.htm?modeId="
					+ supplierDenomination.getSupplierRechargeModeId();
			return "success";
		} catch (ManagerException e) {
			modeId = supplierDenomination.getSupplierRechargeModeId();
			supplierDenomination = null;
			supplierDenomination = new SupplierDenomination();
			supplierDenomination.setSupplierRechargeModeId(modeId);
			addActionError(e.getMessage());
			return this.add();
		} catch (Exception e) {
			addActionError("系统忙！");
			return this.add();
		}

	}

	// 列表
	public String list() {
		if (supplierDenominationQuery == null) {
			supplierDenominationQuery = new SupplierDenominationQuery();
			supplierDenominationQuery.setPageSize(Constants.INIT_PAGESIZE);
			supplierDenominationQuery
					.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		supplierDenominationQuery.setSupplierRechargeModeId(modeId);
		try {
			page = supplierDenominationService.queryList(
					supplierDenominationQuery, getCurrentOperator());
		} catch (Exception e) {
			return ERROR;
		}

		return "list";
	}

	// 编辑
	public String edit() throws Exception {
		if (id != null) {
			supplierDenomination = supplierDenominationService.queryById(id,
					this.getCurrentOperator());
			supplierDenomination.setFaceValue(supplierDenomination
					.getFaceValue());
			supplierDenomination.setCostPrice(supplierDenomination
					.getCostPrice());
			if (supplierDenomination.getMaxFaceValue() != null)
				supplierDenomination.setMaxFaceValue(supplierDenomination
						.getMaxFaceValue());
		}
		return "edit";
	}

	// 编辑供货商提供的充值面额
	public String editDenomination() throws Exception {
		try {
			supplierDenomination.setFaceValue((int) (Double
					.parseDouble(faceValue) * 1000));
			supplierDenomination.setCostPrice((int) (Double
					.parseDouble(costPrice) * 1000));
			if (StringUtils.isNotEmpty(maxFaceValue))
				supplierDenomination.setMaxFaceValue((int) (Double
						.parseDouble(maxFaceValue) * 1000));
			SupplierRechargeMode supplierRechargeMode = new SupplierRechargeMode();
			supplierRechargeMode = supplierRechargeModeService.queryById(
					supplierDenomination.getSupplierRechargeModeId(),
					this.getCurrentOperator());
			supplierDenomination.setOperationCode(supplierRechargeMode
					.getOperationCode());
			supplierDenominationService.update(supplierDenomination,
					this.getCurrentOperator());
			redirectionUrl = "supplier_denomination!list.htm?modeId="
					+ supplierDenomination.getSupplierRechargeModeId();
			return "success";
		} catch (ManagerException e) {
			addActionError(e.getMessage());
			return this.edit();
		}
	}

	// 删除
	public String delete() throws Exception {
		try{
			supplierDenominationService.delete(id,getCurrentOperator());
			redirectionUrl = "supplier_denomination!list.htm?modeId="
					+ modeId;
			return "success";
		}catch(ManagerException e){
			addActionError(e.getMessage());
			return this.list();			
		}
	}
	// 禁用供货商提供的充值面额
	public String enableRecharge() throws Exception {
		try {
			supplierDenominationService.updateRechargeById(id,this.getCurrentOperator());
			return "json";
		} catch (ManagerException e) {
			addActionError(e.getMessage());
			return "json";
		}
	}
}